<?php
/***********************************************
 *      yaf 框架报错类调用
 * 文件: /controller/Error.php
 * 说明: yaf 框架报错类调用
 * 作者: Myxf
 * 更新: 2015年5月15日
 ***********************************************/

/**
 * yaf 框架报错类调用
 */
class ErrorController extends Yaf_Controller_Abstract {
    private $_config;
    public function init(){
        $this->_config = Yaf_Application::app()->getConfig();
    }
    /**
     * [具体错误处理]
     * @param  Exception $exception [description]
     * @return [type]               [description]
     */
    public function errorAction(Exception $exception)
    {
        Yaf_Dispatcher::getInstance()->autoRender(false);
        if ($this->_config->application->showErrors) {
            switch ($exception->getCode()) {
                case YAF_ERR_AUTOLOAD_FAILED:
                case YAF_ERR_NOTFOUND_MODULE:
                case YAF_ERR_NOTFOUND_CONTROLLER:
                case YAF_ERR_NOTFOUND_ACTION:
                case YAF_ERR_NOTFOUND_VIEW:
                    if (strpos($this->getRequest()->getRequestUri(), '.css') !== false ||
                        strpos($this->getRequest()->getRequestUri(), '.jpg') !== false ||
                        strpos($this->getRequest()->getRequestUri(), '.js') !== false ||
                        strpos($this->getRequest()->getRequestUri(), '.png') !== false ||
                        strpos($this->getRequest()->getRequestUri(), '.ico') !== false ||
                        strpos($this->getRequest()->getRequestUri(), '.gif') !== false
                    ) {
                        header('HTTP/1.1 404 Not Found');
                    }
                default:
                    //显示错误信息
                    $this->_view->message =  $exception;
                    echo $this->getView()->render(APP_PATH.'/views/error/error.phtml');
            }
        } else {
            //禁止输出视图内容
            Yaf_Dispatcher::getInstance()->enableView();
            switch ($exception->getCode()) {
                case YAF_ERR_AUTOLOAD_FAILED:
                case YAF_ERR_NOTFOUND_MODULE:
                case YAF_ERR_NOTFOUND_CONTROLLER:
                case YAF_ERR_NOTFOUND_ACTION:
                case YAF_ERR_NOTFOUND_VIEW:
                    header('HTTP/1.1 404 Not Found');
                    $this->_view->type='err404';
                    echo "Page was not found";
                    break;
                default:
                    header('HTTP/1.1 500 Internal Server Error');
                    $this->_view->type='error';
                    echo "Application Error";
                    break;
            }
            exit();
        }
    }
}
